	.module diskio.c
	.area text(rom, con, rel)
	.dbfile D:\01.Projects\JKElectronics\ATMEGA\CoreModule\sw\examples\lib\diskio.c
	.dbfunc e disk_initialize _disk_initialize fc
;          retry -> R20
;          state -> R22
;            drv -> R10
	.even
_disk_initialize::
	xcall push_xgsetF00C
	mov R10,R16
	.dbline -1
	.dbline 30
; /*-----------------------------------------------------------------------*/
; /* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2007        */
; /*-----------------------------------------------------------------------*/
; /* by grqd_xp                                                            */
; /* This is a stub disk I/O module that acts as front end of the existing */
; /* disk I/O modules and attach it to FatFs module with common interface. */
; /*-----------------------------------------------------------------------*/
; #include <string.h>
; 
; //#include "stm32f10x.h"
; #include "hw_config.h"
; 
; #include "diskio.h"
; #include "mmc_sd.h"
; 
; /*-----------------------------------------------------------------------*/
; /* Correspondence between physical drive number and physical drive.      */
; /* Note that Tiny-FatFs supports only single drive and always            */
; /* accesses drive number 0.                                              */
; 
; #define SECTOR_SIZE 512U
; 
; //u32 buff2[512/4];
; /*-----------------------------------------------------------------------*/
; /* Inidialize a Drive                                                    */
; 
; DSTATUS disk_initialize (
; 	byte drv				/* Physical drive nmuber (0..) */
; )
; {
	.dbline 34
; 	u8 retry;
; 	u8 state;
; 
; 	if(drv)
	tst R10
	breq L5
X0:
	.dbline 35
; 	{
	.dbline 36
; 	    return STA_NOINIT;  // only supports the operation of the disk 0
	ldi R16,1
	xjmp L4
L5:
	.dbline 39
; 	}
; 
; 	retry = 0;
	clr R20
L7:
	.dbline 42
; 
; 	do
; 	{
	.dbline 43
; 		state = SD_Init();
	xcall _SD_Init
	mov R22,R16
	.dbline 44
; 		if(state == STA_NODISK)
	cpi R16,2
	brne L10
X1:
	.dbline 45
; 		{
	.dbline 46
; 		    return STA_NODISK;
	ldi R16,2
	xjmp L4
L10:
	.dbline 48
; 		}
; 		else if(state != 0)
	tst R22
	breq L12
X2:
	.dbline 49
; 		{
	.dbline 50
; 			if( retry > 19 )
	ldi R24,19
	cp R24,R20
	brsh L13
X3:
	.dbline 51
; 			    return STA_NOINIT;  // other error: initialization failed
	ldi R16,1
	xjmp L4
L12:
	.dbline 54
; 		}
; 		else
; 		{
	.dbline 55
; 		    return 0;           // initialization succeeded
	clr R16
	xjmp L4
L13:
	.dbline 57
; 		}
; 		retry++;
	inc R20
	.dbline 58
; 	} while ((state != 0x00) && (retry < 20));
L8:
	.dbline 58
	tst R22
	breq L16
X4:
	cpi R20,20
	brlo L7
X5:
L16:
	.dbline -2
L4:
	.dbline 0 ; func end
	xjmp pop_xgsetF00C
	.dbsym r retry 20 c
	.dbsym r state 22 c
	.dbsym r drv 10 c
	.dbend
	.dbfunc e disk_status _disk_status fc
;            drv -> R16
	.even
_disk_status::
	.dbline -1
	.dbline 69
; }
; 
; 
; 
; /*-----------------------------------------------------------------------*/
; /* Return Disk Status                                                    */
; 
; DSTATUS disk_status (
; 	byte drv		/* Physical drive nmuber (0..) */
; )
; {	
	.dbline 70
;     if(drv)
	tst R16
	breq L18
X6:
	.dbline 71
;     {
	.dbline 72
;         return STA_NOINIT;  // only supports disk-0 operation
	ldi R16,1
	xjmp L17
L18:
	.dbline 80
;     }
; 
; 	// Check whether the inserted SD card
; 	//  if(!SD_DET())
; 	//  {
; 	//     return STA_NODISK;
; 	//  }
; 	return 0;
	clr R16
	.dbline -2
L17:
	.dbline 0 ; func end
	ret
	.dbsym r drv 16 c
	.dbend
	.dbfunc e disk_read _disk_read fc
;            res -> R10
;          count -> R20
;         sector -> y+11
;           buff -> R12,R13
;            drv -> R14
	.even
_disk_read::
	xcall push_xgset30FC
	movw R12,R18
	mov R14,R16
	sbiw R28,3
	ldd R20,y+15
	.dbline -1
	.dbline 94
; }
; 
; 
; 
; /*-----------------------------------------------------------------------*/
; /* Read Sector(s)                                                        */
; 
; DRESULT disk_read (
; 	byte drv,		/* Physical drive nmuber (0..) */
; 	byte *buff,		/* Data buffer to store read data */
; 	u32 sector,	/* Sector address (LBA) */
; 	byte count		/* Number of sectors to read (1..255) */
; )
; {
	.dbline 95
; 	u8 res=0;
	clr R10
	.dbline 96
;     if (drv || !count)
	tst R14
	brne L23
X7:
	tst R20
	brne L21
X8:
L23:
	.dbline 97
;     {    
	.dbline 98
;         return RES_PARERR;  // only supports single disk operation, count is not equal to 0, otherwise parameter error
	ldi R16,4
	xjmp L20
L21:
	.dbline 107
;     }
;    // if(!SD_DET())
;    // {
;    //     return RES_NOTRDY;  // does not detect SD card, NOT READY error reported
;    // }
; 
;     
; 	
;     if(count==1) // sector reads 1
	cpi R20,1
	brne L24
X9:
	.dbline 108
;     {                                                
	.dbline 109
;         res = SD_ReadSingleBlock(sector, buff);      
	std y+1,R13
	std y+0,R12
	ldd R16,y+11
	ldd R17,y+12
	ldd R18,y+13
	ldd R19,y+14
	xcall _SD_ReadSingleBlock
	mov R10,R16
	.dbline 110
;     }                                                
	xjmp L25
L24:
	.dbline 112
;     else // multiple sector read operations
;     {                                                
	.dbline 113
;         res = SD_ReadMultiBlock(sector, buff, count);
	std y+2,R20
	std y+1,R13
	std y+0,R12
	ldd R16,y+11
	ldd R17,y+12
	ldd R18,y+13
	ldd R19,y+14
	xcall _SD_ReadMultiBlock
	mov R10,R16
	.dbline 114
;     }                                                
L25:
	.dbline 127
; 	/*
;     do                           
;     {                                          
;         if(SD_ReadSingleBlock(sector, buff)!=0)
;         {                                      
;             res = 1;                           
;             break;                             
;         }                                      
;         buff+=512;                             
;     }while(--count);                                         
;     */
;     // Process the return value, the return value of the SPI_SD_driver.c the return value turned into ff.c
;     if(res == 0x00)
	tst R10
	brne L26
X10:
	.dbline 128
;     {
	.dbline 129
;         return RES_OK;
	clr R16
	xjmp L20
L26:
	.dbline 132
;     }
;     else
;     {
	.dbline 133
;         return RES_ERROR;
	ldi R16,1
	.dbline -2
L20:
	.dbline 0 ; func end
	adiw R28,3
	xjmp pop_xgset30FC
	.dbsym r res 10 c
	.dbsym r count 20 c
	.dbsym l sector 11 l
	.dbsym r buff 12 pc
	.dbsym r drv 14 c
	.dbend
	.dbfunc e disk_write _disk_write fc
;            res -> R10
;          count -> R20
;         sector -> y+9
;           buff -> R12,R13
;            drv -> R10
	.even
_disk_write::
	xcall push_xgset303C
	movw R12,R18
	mov R10,R16
	sbiw R28,3
	ldd R20,y+13
	.dbline -1
	.dbline 149
;     }
; }
; 
; 
; 
; /*-----------------------------------------------------------------------*/
; /* Write Sector(s)                                                       */
; 
; #if _READONLY == 0
; DRESULT disk_write (
; 	byte drv,			/* Physical drive nmuber (0..) */
; 	const byte *buff,	/* Data to be written */
; 	u32 sector,		/* Sector address (LBA) */
; 	byte count			/* Number of sectors to write (1..255) */
; )
; {
	.dbline 152
; 	u8 res;
; 
;     if (drv || !count)
	tst R10
	brne L31
X11:
	tst R20
	brne L29
X12:
L31:
	.dbline 153
;     {    
	.dbline 154
;         return RES_PARERR;  // only supports single disk operation, count is not equal to 0, otherwise parameter error
	ldi R16,4
	xjmp L28
L29:
	.dbline 163
;     }
; 	/*
;     if(!SD_DET())
;     {
;         return RES_NOTRDY;  // does not detect SD card, NOT READY error reported
;     }  */
; 
;     // Read and write operations
;     if(count == 1)
	cpi R20,1
	brne L32
X13:
	.dbline 164
;     {
	.dbline 165
;         res = SD_WriteSingleBlock(sector, buff);
	std y+1,R13
	std y+0,R12
	ldd R16,y+9
	ldd R17,y+10
	ldd R18,y+11
	ldd R19,y+12
	xcall _SD_WriteSingleBlock
	mov R10,R16
	.dbline 166
;     }
	xjmp L33
L32:
	.dbline 168
;     else
;     {
	.dbline 169
;         res = SD_WriteMultiBlock(sector, buff, count);
	std y+2,R20
	std y+1,R13
	std y+0,R12
	ldd R16,y+9
	ldd R17,y+10
	ldd R18,y+11
	ldd R19,y+12
	xcall _SD_WriteMultiBlock
	mov R10,R16
	.dbline 170
;     }
L33:
	.dbline 172
;     // Return value to
;     if(res == 0)
	tst R10
	brne L34
X14:
	.dbline 173
;     {
	.dbline 174
;         return RES_OK;
	clr R16
	xjmp L28
L34:
	.dbline 177
;     }
;     else
;     {
	.dbline 178
;         return RES_ERROR;
	ldi R16,1
	.dbline -2
L28:
	.dbline 0 ; func end
	adiw R28,3
	xjmp pop_xgset303C
	.dbsym r res 10 c
	.dbsym r count 20 c
	.dbsym l sector 9 l
	.dbsym r buff 12 pkc
	.dbsym r drv 10 c
	.dbend
	.dbfunc e disk_ioctl _disk_ioctl fc
;            res -> R20
;           buff -> R10,R11
;           ctrl -> R12
;            drv -> R14
	.even
_disk_ioctl::
	xcall push_xgset30FC
	mov R12,R18
	mov R14,R16
	ldd R10,y+8
	ldd R11,y+9
	.dbline -1
	.dbline 193
;     }
; }
; #endif /* _READONLY */
; 
; 
; 
; /*-----------------------------------------------------------------------*/
; /* Miscellaneous Functions                                               */
; 
; DRESULT disk_ioctl (
; 	byte drv,		/* Physical drive nmuber (0..) */
; 	byte ctrl,		/* Control code */
; 	void *buff		/* Buffer to send/receive control data */
; )
; {
	.dbline 198
; 
;     DRESULT res;
; 
; 
;     if (drv)
	tst R14
	breq L37
X15:
	.dbline 199
;     {    
	.dbline 200
;         return RES_PARERR;  // only supports single disk operation, or return parameter error
	ldi R16,4
	xjmp L36
L37:
	.dbline 204
;     }
;     
;     // FATFS only deal with the current version of CTRL_SYNC, GET_SECTOR_COUNT, GET_BLOCK_SIZ three commands
;     switch(ctrl)
	mov R20,R12
	clr R21
	cpi R20,0
	cpc R20,R21
	breq L42
X16:
	cpi R20,1
	ldi R30,0
	cpc R21,R30
	breq L46
X17:
	cpi R20,3
	ldi R30,0
	cpc R21,R30
	breq L45
X18:
	xjmp L39
L42:
	.dbline 207
;     {
;     case CTRL_SYNC:
;         MSD_CS_ENABLE();
	cbi 0x18,0
	.dbline 208
;         if(SD_WaitReady()==0)
	xcall _SD_WaitReady
	tst R16
	brne L43
X19:
	.dbline 209
;         {
	.dbline 210
;             res = RES_OK;
	clr R20
	.dbline 211
;         }
	xjmp L44
L43:
	.dbline 213
;         else
;         {
	.dbline 214
;             res = RES_ERROR;
	ldi R20,1
	.dbline 215
;         }
L44:
	.dbline 216
;         MSD_CS_DISABLE();
	sbi 0x18,0
	.dbline 217
;         break;
	xjmp L40
L45:
	.dbline 220
;         
;     case GET_BLOCK_SIZE:
;         *(u16*)buff = 512;
	ldi R24,512
	ldi R25,2
	movw R30,R10
	std z+1,R25
	std z+0,R24
	.dbline 221
;         res = RES_OK;
	clr R20
	.dbline 222
;         break;
	xjmp L40
L46:
	.dbline 225
; 
;     case GET_SECTOR_COUNT:
;         *(u32*)buff = SD_GetCapacity();
	xcall _SD_GetCapacity
	movw R30,R10
	std z+0,R16
	std z+1,R17
	std z+2,R18
	std z+3,R19
	.dbline 226
;         res = RES_OK;
	clr R20
	.dbline 227
;         break;
	xjmp L40
L39:
	.dbline 229
;     default:
;         res = RES_PARERR;
	ldi R20,4
	.dbline 230
;         break;
L40:
	.dbline 233
;     }
; 
;     return res;
	mov R16,R20
	.dbline -2
L36:
	.dbline 0 ; func end
	xjmp pop_xgset30FC
	.dbsym r res 20 c
	.dbsym r buff 10 pV
	.dbsym r ctrl 12 c
	.dbsym r drv 14 c
	.dbend
	.dbfunc e get_fattime _get_fattime fl
	.even
_get_fattime::
	.dbline -1
	.dbline 236
; }
; 
; u32 get_fattime(void){
	.dbline 237
; 	return 0;
	ldi R16,0
	ldi R17,0
	ldi R18,0
	ldi R19,0
	.dbline -2
L47:
	.dbline 0 ; func end
	ret
	.dbend
; }
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
